package com.accounting.client.frontend.system;

import java.util.HashMap;
import java.util.Map;

import com.accounting.client.ApplicationContext;
import com.accounting.client.remote.Services;
import com.accounting.client.remote.obj.Setting;
import com.accounting.client.remote.obj.SettingKey;
import com.accounting.client.remote.transfer.UpdateSettingsRequest;
import com.accounting.client.resource.BoxResources;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.delegate.CompleteCallback;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.DecimalBox;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.layout.LabelAlignment;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.layout.RelativeLayout;

public class SettingsHandler extends SimpleDialogHandler {
	
	private DecimalBox rateField;
	private DecimalBox realRateField;
	private DecimalBox balanceField;

	@Override
	public String getHeading() {
		return "系统设置";
	}

	@Override
	public Size getSize() {
		return new Size(400, 250);
	}

	@Override
	public Widget getContentPanel() {
		rateField     = new DecimalBox();
		realRateField = new DecimalBox();
		balanceField  = new DecimalBox();
		
		int fw = 250;
		int lw = 100;
		
		RelativeLayout layout = RelativeLayout.create(fw, LabelAlignment.Left, lw);
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		RelativeColumn column = new RelativeColumn();
		column.setRowSpacing(20);
		
		column.addRow().add(new SimpleLabel("当前汇率").setBold(true),	  realRateField, layout);
		column.addRow().add(new SimpleLabel("计算利润汇率").setBold(true),  rateField, layout);
		column.addRow().add(new SimpleLabel("账户余额").setBold(true),     balanceField, layout);
		column.addRow().add(new Button("更新系统设置",  BoxResources.INSTANCE.cube16(),100, new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				update();
			}
		}), 100);
		
		panel.add(column.getPanel());
		return panel;
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[0];
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}

	@Override
	public void onShowing() {
		Setting rateSetting 	= ApplicationContext.getSetting(SettingKey.Rate.getName());
		Setting realRateSetting = ApplicationContext.getSetting(SettingKey.RealRate.getName());
		Setting balanceSetting 	= ApplicationContext.getSetting(SettingKey.Balance.getName());
		
		this.rateField.setDouble(Util.getDouble(rateSetting.getValue(), 0.0), false);
		this.realRateField.setDouble(Util.getDouble(realRateSetting.getValue(), 0.0), false);
		this.balanceField.setDouble(Util.getDouble(balanceSetting.getValue(), 0.0), false);
	}
	
	private void update() {
		
		Map<String,String> settings = new HashMap<String,String>();
		if (rateField.getDouble(0.0) != 0) {
			settings.put(SettingKey.Rate.getName(), Util.getString(rateField.getDouble(0.0)));
		}
		if (realRateField.getDouble(0.0) != 0) {
			settings.put(SettingKey.RealRate.getName(), Util.getString(realRateField.getDouble(0.0)));
		}
		if (balanceField.getDouble(0.0) != 0) {
			settings.put(SettingKey.Balance.getName(), Util.getString(balanceField.getDouble(0.0)));
		}
		
		UpdateSettingsRequest request = new UpdateSettingsRequest();
		request.setSettings(settings);
		
		String passport = SessionManager.getPassport();
		Services.SettingService.UpdateSettings(passport, request, new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				ErrorHandler.handleServiceError(caught);
			}
			
			@Override
			public void onSuccess(Boolean result) {
				if (result == Boolean.TRUE) {
					ApplicationContext.refreshSettings(new CompleteCallback(){
						@Override
						public void onCompleted() {
							closeDialog();
						}
					});	
				}
			}
		});
	}

}
